package com.server;

import java.io.DataOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import org.json.JSONException;
import org.json.JSONObject;

public class server_chatRoom_manager{	
	public server_chatRoom_manager(){		
		userList = new HashMap<Long, DataOutputStream>();		
		roomList = new HashMap<Long,ArrayList<Long> >();
	}
	
	public void login(Long userID,DataOutputStream dos){
		if(isOnline(userID))
			userList.remove(userID);	
		userList.put(userID, dos);		
	}
	public void addNewRoom( Long roomID,Long userID){
		ArrayList<Long> newRoom = new ArrayList<Long>();
		newRoom.add(userID);
		roomList.put(roomID, newRoom);
		System.out.println("new room ID:" + roomID);
	}
	public int getRoomSize(Long roomID){
		return roomList.get(roomID).size();
	}
	public void addNewMember( Long roomID,Long userID){		
		if(!roomList.containsKey(roomID)){
			addNewRoom(roomID, userID);
		}
		else{
			ArrayList<Long> room = null;
			room = roomList.get(roomID);
			room.add(userID);
			roomList.put(roomID, room);
			System.out.println("room size:" + room.size());
		}		
	}
	public void sendAllMessageNotSell( Long roomID, Long userID,String msg){
		ArrayList<Long> room = roomList.get(roomID);
//		Long room_userID = null;
		DataOutputStream dos = null;		
		for(Long room_userID : room){
			if(room_userID - userID != 0){
				try {
					dos = userList.get(room_userID);
			//		obj.writeBytes(msg);
					if(dos != null)
						dos.write(msg.getBytes());
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}	
		}
/*		Iterator iter = room.iterator();
		while(iter.hasNext()){
			room_userID = (Long)iter.next();
			if(room_userID - userID != 0){
				try {
					obj = userList.get(room_userID);
					obj.writeBytes(msg);
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}*/
	}

	public void sendAllMessage(Long roomID, Long userID,String msg){
		ArrayList<Long> room = roomList.get(roomID);
		DataOutputStream dos = null;	
		for(Long room_userID : room){
			try {
				dos = userList.get(room_userID);
		//		obj.writeBytes(msg);
				if(dos != null)
					dos.write(msg.getBytes());
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}				
		}		
	}
	public void sendOneMessage(String msg,DataOutputStream dos){
		try {		
			dos.write(msg.getBytes());
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public JSONObject getRoommate(Long roomID){
		ArrayList<Long> room = roomList.get(roomID);
		int i = 0;		
		JSONObject json = new JSONObject();
//		Iterator iter = room.iterator();		
		try {
			for(Long room_userID : room){
				Map map = new HashMap();
				map.put("ui", room_userID);	
				json.put(++i + "", map);	
			}			
/*			while(iter.hasNext()){
				room_userID = (Long) iter.next();
				Map map = new HashMap();
				map.put("ui", room_userID);	
				json.put(++i + "", map);
			}*/
			json.put("total", i);
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return json;
	}
	
	public boolean isInRoom(Long userID,Long roomID){
		boolean result = false;
		if(roomList.containsKey(roomID)){
			ArrayList<Long> room = roomList.get(roomID);
			if(room.contains(userID))
				result = true;
		}
		return result;
	}
	
	public boolean isOnline(Long userID){
		return userList.containsKey(userID);
	}
	
	public DataOutputStream getDOS(Long userID){
		if(userList.containsKey(userID)){
			return (DataOutputStream)userList.get(userID);
		}
		else 
			return null;
	}
	
	public void removeRoomMember(Long userID,Long roomID){
		ArrayList<Long> room = roomList.get(roomID);
		room.remove(userID);	
		System.out.println(room.size());
	}
	
	public void removeMember(Long userID){
		userList.remove(userID);
		System.out.println(userList.size());
	}
	
	public JSONObject getInRoomList(Long userID){
		JSONObject json = new JSONObject();
		int i = 0;
		Iterator iter = roomList.entrySet().iterator();
		try {
			while(iter.hasNext()){
				Map.Entry<Long, ArrayList<Long>> entry = (Map.Entry<Long, ArrayList<Long>>) iter.next();
				ArrayList<Long> arrayList = entry.getValue();
				if(arrayList.contains(userID)){
					JSONObject roomJSON = new JSONObject();
					roomJSON.put("ri", entry.getKey());
					json.put(++i + "", roomJSON);
				}
			}			
			json.put("total", i);
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return json;
	}
	
	private Map<Long,ArrayList<Long> > roomList;
	private Map<Long,DataOutputStream> userList;
}